package org.study.swing.study11.example;

import cn.hutool.core.util.JdkUtil;
import com.baomidou.mybatisplus.core.MybatisPlusVersion;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringBootVersion;
import org.springframework.core.SpringVersion;
import org.springframework.stereotype.Component;
import org.study.swing.study11.mapper.TestMapper;

import javax.sql.DataSource;
import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.util.LinkedHashMap;

/**
 * 参考 https://www.yisu.com/ask/4977886.html
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2024年11月12日
 */
@Slf4j
@Component
public class MyApplicationFrame extends JFrame {
    public MyApplicationFrame(DataSource dataSource, TestMapper testMapper) {
        String sql = "SELECT 1 FROM DUAL";
        try (Connection connection = dataSource.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
             ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                int result = resultSet.getInt(1);
                log.info("检测数据库连接，执行SQL：{}，结果：{}", sql, result);
            }
        } catch (SQLException ex) {
            log.error("检测数据库连接异常", ex);
            System.exit(0);
        }
        Integer checkDefault = testMapper.checkConnectionDefault();
        log.info("检测数据库连接，结果：{}", checkDefault);

        LinkedHashMap<String, Object> checkBySql = testMapper.checkConnectionBySql(sql);
        log.info("检测数据库连接，执行SQL：{}，结果：{}", sql, checkBySql);

        // 初始化Swing组件和布局
        Container container = super.getContentPane();
        container.setLayout(new GridLayout(15, 1));

        container.add(new JLabel("SpringBoot 与 Swing 集成示例", JLabel.CENTER));
        container.add(new JLabel("JDK 版本：" + JdkUtil.JVM_VERSION, JLabel.LEFT));
        container.add(new JLabel("MybatisPlus 版本：" + MybatisPlusVersion.getVersion(), JLabel.LEFT));
        container.add(new JLabel("Spring 版本：" + SpringVersion.getVersion(), JLabel.LEFT));
        container.add(new JLabel("SpringBoot 版本：" + SpringBootVersion.getVersion(), JLabel.LEFT));

        /*// 设置窗口标题
        super.setTitle("SpringBoot 与 Swing 集成示例");
        // 设置窗口大小
        super.setSize(600, 400);
        // 将窗口居中显示在屏幕上
        super.setLocationRelativeTo(null);
        super.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        super.setVisible(true);*/
    }
}
